package ztc.service;

import ztc.common.DBcon;
import ztc.common.SiginBean;
import ztc.common.StuBean;
import ztc.common.UserBean;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ztc on 16-4-8.下午9:26
 */
public class DBUsers {
    public boolean regUser(UserBean user){
        String sql="insert into users(name,sex,email,password,qq,weixin,phone,time) values(?,?,?,?,?,?,?,?);";
        Connection con= DBcon.getCon();
        PreparedStatement pre;
        try {
            pre=con.prepareStatement(sql);
            pre.setString(1,user.getName());
            pre.setString(2,user.getSex());
            pre.setString(3,user.getEmail());
            pre.setString(4,user.getPassword());
            pre.setString(5,user.getQq());
            pre.setString(6,user.getWeixin());
            pre.setString(7,user.getPhone());
            pre.setString(8,user.getTime());
            int result=pre.executeUpdate();
            pre.close();
            con.close();
            return result==1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public boolean regStudent(StuBean stu){
        String sql="insert into students values(?,?,?,?,?,?,?);";
        Connection con=DBcon.getCon();
        PreparedStatement pr;
        try {
            pr=con.prepareStatement(sql);
            pr.setString(1,stu.getStuID());
            pr.setString(2,stu.getName());
            pr.setString(3,stu.getSex());
            pr.setString(4,stu.getClasss());
            pr.setString(5,stu.getInstitute());
            pr.setString(6,stu.getMac());
            pr.setString(7,stu.getTime());
            int result=pr.executeUpdate();
            pr.close();
            con.close();
            return result==1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public UserBean login(String email,String password){
        String sql="select*from users where email=? and password=?;";
        Connection con=DBcon.getCon();
        UserBean user=null;
        try {
            PreparedStatement pr=con.prepareStatement(sql);
            pr.setString(1,email);
            pr.setString(2,password);
            ResultSet resultSet=pr.executeQuery();
            if(resultSet.next()) {
                user= new UserBean(resultSet.getInt("id"),
                        resultSet.getString("name"),
                        resultSet.getString("sex"),
                        resultSet.getString("email"),
                        resultSet.getString("password"),
                        resultSet.getString("qq"),
                        resultSet.getString("weixin"),
                        resultSet.getString("phone"));
            }
            pr.close();
            con.close();
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null ;
    }
    private void insertstu(){
        String sql="insert into students values(?,'张三','男','网络132','计算机科学与技术学院','mac','time');";
        Connection con=DBcon.getCon();
        PreparedStatement pr;
        try {
            pr=con.prepareStatement(sql);
            for(int i=1313042030;i<1313042043;i++){
                pr.setString(1,String.valueOf(i));
                pr.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    /*
    public static void main(String[] args){
        new DBUsers().insertstu();
    }
    */
}
